package com.aaa.tars.service.impl;

import com.aaa.tars.dao.SaleDao;
import com.aaa.tars.entity.Sale;
import com.aaa.tars.service.SaleService;
import com.aaa.tars.util.CustomException;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @FileName: SaleServiceImpl
 * @Description:
 * @Author: ghz
 * @CreateTime:2025/9/9 15:10
 * @Version:1.0.0
 */
@Service
public class SaleServiceImpl implements SaleService {
    @Resource
    private SaleDao saleDao;
    public Sale queryById(Integer saleId){
        if (saleId== null|| saleId<=0){
            throw  new CustomException(ResultStatus.ERROR.getStatusCode(),ResultStatus.ERROR.getStatusMessage());
        }
        return saleDao.queryById(saleId);
    }

    public PageInfo queryByPage(Page<Sale> page){
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageNo <= 0 || pageSize == null || pageSize <= 0){
            throw new CustomException(ResultStatus.ERROR.getStatusCode(),ResultStatus.ERROR.getStatusMessage());
        }
        PageHelper.startPage(pageNo,pageSize);
        Sale sale = page.getData();
        return new PageInfo(saleDao.queryByParam(sale));
    }
    public int insert(Sale sale){
        sale.setSalePlacetime(new Date());
        return saleDao.insert(sale);
    }

    public int update(Sale sale){
        return saleDao.update(sale);
    }
    public int deleteById(Integer saleId){
        if (saleId== null|| saleId<=0){
            throw  new CustomException(ResultStatus.ERROR.getStatusCode(),ResultStatus.ERROR.getStatusMessage());
        }
        return saleDao.deleteById(saleId);
    }

}
